﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        public void YGRVV20LISTV(DataSet db)
        {
            exAgent.SetCurrentSheet("YGRVV20LIST");
        }
        public void YGRVV20LIST(DataSet db)
        {
            string msg = "";
            string sdkey = "";
            string src = "";
            try
            {
                DataTable tbMain = tbExport.Clone();
                tbExport.Clear();
                for (int i = 0; i < 2000; i++)
                {
                    tbMain.Rows.Add();
                }
                #region======初始化=========
                tbMain.Rows[0][0] = "巢湖新恒生纺织有限公司";
                tbMain.Rows[1][0] = "成品纱公路运输货物运单";
                tbMain.Rows[6][0] = "托运人";
                tbMain.Rows[6][3] = "巢湖新恒生纺织有限公司";
                tbMain.Rows[6][23] = "地址";
                tbMain.Rows[6][25] = "安徽省巢湖市临湖路65#";
                tbMain.Rows[6][35] = "电话";
                tbMain.Rows[6][39] = "13856570360";
                tbMain.Rows[8][0] = "承运人";
                tbMain.Rows[8][3] = "";
                tbMain.Rows[8][23] = "地址";
                tbMain.Rows[8][35] = "电话";
                tbMain.Rows[9][0] = "装货地点";
                tbMain.Rows[9][3] = "安徽省巢湖市临湖路65#";
                tbMain.Rows[9][23] = "起运时间";
                tbMain.Rows[9][35] = "司机姓名";
                tbMain.Rows[10][35] = "车牌号";
                tbMain.Rows[11][0] = "卸货地点";
                //tbMain.Rows[11][3] = "安徽省巢湖市临湖路65#";
                tbMain.Rows[11][23] = "到达时间";
                tbMain.Rows[11][35] = "身份证";

                tbMain.Rows[12][35] = "住址";
                tbMain.Rows[14][0] = "序号";
                tbMain.Rows[14][2] = "申购单号";
                tbMain.Rows[14][7] = "纱线规格";
                tbMain.Rows[14][11] = "色号";
                tbMain.Rows[14][17] = "批号";
                tbMain.Rows[14][22] = "编织袋+打包带";
                tbMain.Rows[14][27] = "包装形式";
                tbMain.Rows[14][29] = "件数";
                tbMain.Rows[14][31] = "单件净重(KG)";
                tbMain.Rows[14][33] = "总净重(KG)";

                tbMain.Rows[14][36] = "货物总值(RMB/元)";
                tbMain.Rows[14][40] = "单件毛重(KG)";
                tbMain.Rows[14][42] = "总毛重(KG)";
                tbMain.Rows[14][46] = "收货方";
                tbMain.Rows[14][50] = "合同号";
                tbMain.Rows[14][56] = "单价";
                tbMain.Rows[14][57] = "小缸费";
                tbMain.Rows[14][59] = "运输编号";
                tbMain.Rows[14][60] = "发货单号";
                tbMain.Rows[14][61] = "销售单号";


                tbMain.Rows[1309][0] = "合计";
                tbMain.Rows[1313][0] = "托运人记载事项";
                tbMain.Rows[1313][33] = "承运人记载事项";
                tbMain.Rows[1315][0] = " 联系人：";
                tbMain.Rows[1316][0] = "手机号：";
                tbMain.Rows[1317][0] = "座机：";
                tbMain.Rows[1318][0] = "托运方签章";

                tbMain.Rows[1314][33] = " 单位：";
                tbMain.Rows[1315][33] = " 车架号：";
                tbMain.Rows[1316][33] = "发电机号；";
                tbMain.Rows[1317][33] = "司机电话：";
                tbMain.Rows[1318][33] = "承运人签章";
                tbMain.Rows[1322][33] = " 注   意   事   项";
                tbMain.Rows[1322][12] = "  1、货物名称应填写具体品名，如货物品名过多，不能再运单内逐一填写，须另附物品清单。 	";
                tbMain.Rows[1323][12] = "2、保险货物，在相应价格中填写货物声明的投保价格。";
                tbMain.Rows[1324][12] = "3、承运人必须保证车厢内清洁，使承运货物不被污染。";
                tbMain.Rows[1325][12] = "4、货物在捆绑、运输等过程中，不得有摔烂、淋雨、破损等现象，否则承运 人将对造成的损失按价赔偿。";
                tbMain.Rows[1325][12] = "5、此单托运、承运双方签字后，作为承运方收到托运方货物的凭证。";

                tbMain.Rows[1327][3] = "说明：";
                tbMain.Rows[1328][3] = " 1、填在一张货物运单内的货物必须是属同一托运人。对拼装分卸货物，应将每一拼装或分卸情况在运单记事，栏内注明。";
                tbMain.Rows[1329][3] = " 易腐、易碎货物，易溢漏的液体，危险货物与普通货物以及性质相  抵触，运输条件不同的货物，";
                tbMain.Rows[1330][3] = "2、不得用同一张运单托运。";
                tbMain.Rows[1331][3] = "3、托运人、承运人修改运单时，须双方签字盖章。";

                #endregion
                exAgent.SetCurrentSheet("YGRVV20LIST");
                DataRow dwVV = dsSource.Tables["VVLIEF"].Rows[0];
                string order = "";
                #region=============Header==================
                tbMain.Rows[3]["F36"] = year + "年" + month + "月" + day + "日";
                //exAgent.SetCellValue(4, 36, year + "年" + month + "月" + day + "日");
                order = Helper.GetSubRollNum(dwVV["VVAVISNR"].ToString());
                if (order == "00000000")
                {
                    order = Helper.GetSubRollNum(dwVV["VVLIEFSCH"].ToString());
                }
                order = Helper.GetSubRollNum(dwVV["VVLIEFSCH"].ToString());
                tbMain.Rows[5]["F40"] = "CH" + year + "-X" + order;
                //exAgent.SetCellValue(6, 40, "CH" + year + "-X" + order);
                //VVSTATUS
                string VVSTATUS = dwVV["VVSTATUS"].ToString();
                if (VVSTATUS == "EN")
                {
                    tbMain.Rows[3]["F01"] = "装货通知单";
                    //exAgent.SetCellValue(4, 1, "装货通知单", true);
                }
                //string 
                //tbMain.Rows[3]["F01"] ="装货通知单",;
                tbMain.Rows[10]["F40"] = GetGB2312Value(dwVV["VVBZBEZU"].ToString());
                tbMain.Rows[9]["F26"] = year + "年" + month + "月" + day + "日";
                //exAgent.SetCellValue(11, 40, GetGB2312Value(dwVV["VVBZBEZU"].ToString()));
                //exAgent.SetCellValue(10, 26, year + "年" + month + "月" + day + "日");
                string VVEBDAT = dwVV["VVEBDAT"].ToString();
                string str1 = "", str2 = "", str3 = "";
                Helper.GetSplitDate(ref str1, ref str2, ref str3, VVEBDAT);
                //tbMain.Rows[3]["F01"] ="装货通知单";
                tbMain.Rows[11]["F26"] = str1;
                tbMain.Rows[9]["F40"] = GetGB2312Value(dwVV["VVORTU"].ToString());
                //exAgent.SetCellValue(12, 26, str1);
                //exAgent.SetCellValue(10, 40, GetGB2312Value(dwVV["VVORTU"].ToString()));
                string VVSEQUNO = dwVV["VVSEQUNO"].ToString();
                if (VVSEQUNO.Trim() != "000000000000000")
                {
                    tbMain.Rows[9]["F36"] = "快递单号";
                    tbMain.Rows[9]["F40"] = Helper.GetSubRollNum(VVSEQUNO);
                    //exAgent.SetCellValue(10, 36, "快递单号");
                    //exAgent.SetCellValue(10, 40, GetGB2312Value(VVSEQUNO));
                }
                #region=====Buyer======
                var qkd = from dwTemp in db.Tables["KUNDADR"].AsEnumerable()
                          where dwTemp["VVLIEFSCH"].ToString() == dwVV["VVLIEFSCH"].ToString()
                          select dwTemp;
                foreach (DataRow dwKD03 in qkd)
                {
                    tbMain.Rows[11]["F04"] = Helper.GetGB2312Value(dwKD03["KDASTRASSE1"].ToString().Trim() + dwKD03["KDASTRASSE2"].ToString().Trim());
                    tbMain.Rows[1315]["F01"] = "联系人:" + Helper.GetGB2312Value(dwKD03["KDAPLZPOSTF"].ToString().Trim());
                    tbMain.Rows[1316]["F01"] = "手机号:" + Helper.GetGB2312Value(dwKD03["KDATTEX"].ToString().Trim());
                    tbMain.Rows[1317]["F01"] = "座机:" + Helper.GetGB2312Value(dwKD03["KDATEL"].ToString().Trim());
                    //exAgent.SetCellValue(12, 4, Helper.GetGB2312Value(dwKD03["KDASTRASSE1"].ToString().Trim() + dwKD03["KDASTRASSE2"].ToString().Trim()));
                    //exAgent.SetCellValue(200, 1, "联系人:" + Helper.GetGB2312Value(dwKD03["KDAPLZPOSTF"].ToString().Trim()));
                    //exAgent.SetCellValue(201, 1, "手机号:" + Helper.GetGB2312Value(dwKD03["KDATEL"].ToString().Trim()));
                    //exAgent.SetCellValue(202, 1, "座机:" + Helper.GetGB2312Value(dwKD03["KDATTEX"].ToString().Trim()));
                    break;
                }
                if (qkd.Count() == 0)
                {
                    qkd = from dwTemp in db.Tables["KDSTAMM"].AsEnumerable()
                          where dwTemp["VVLIEFSCH"].ToString() == dwVV["VVLIEFSCH"].ToString()
                          select dwTemp;
                    foreach (DataRow dwKD03 in qkd)
                    {
                        tbMain.Rows[11]["F04"] = Helper.GetGB2312Value(dwKD03["KDSTRASSE1"].ToString().Trim() + dwKD03["KDSTRASSE2"].ToString().Trim());
                        //exAgent.SetCellValue(12, 4, Helper.GetGB2312Value(dwKD03["KDSTRASSE1"].ToString().Trim() + dwKD03["KDSTRASSE2"].ToString().Trim()));
                        break;
                    }
                }
                qkd = from dwTemp in dsSource.Tables["KDANSP"].AsEnumerable()
                      where dwTemp["VVLIEFSCH"].ToString() == dwVV["VVLIEFSCH"].ToString()
                      select dwTemp;
                foreach (DataRow dwKD13 in qkd)
                {
                    //   src = "客户编号/Client No:";
                    //exAgent.SetCellValue(8, 30, src); 
                    string name = Helper.Convert8859P1ToGB2312(dwKD13["KDSANSPNAME"].ToString()).Trim();
                    string phone = dwKD13["KDSTEL"].ToString().Trim();
                    //tbMain.Rows[9]["F36"] = "快递单号";
                    //tbMain.Rows[9]["F36"] = "快递单号";
                    //exAgent.SetCellValue(200, 1, "联系人:" + name);
                    //exAgent.SetCellValue(201, 1, "手机号:" + phone);
                }
                DataRow dwKD = null;
                qkd = from dwTemp in db.Tables["KDSTAMM"].AsEnumerable()
                      where dwTemp["VVLIEFSCH"].ToString() == dwVV["VVLIEFSCH"].ToString()
                      select dwTemp;
                foreach (DataRow dwKD03 in qkd)
                {
                    dwKD = dwKD03;
                }
                if (dwKD == null)
                    return;
                #endregion
                #endregion

                int xStart = 18;
                int rown = 2;
                int idx = 0;
                string finish = "";
                int box = 0;
                double tgross = 0, tnet = 0, tpackages = 0;
                DataTable tbVVT = db.Tables["VVTLIEF"];
                var qVV = from VVT in db.Tables["PVVLIEF"].AsEnumerable()
                          orderby VVT.Field<string>("GPASPPART") descending
                          select VVT;
                int count = qVV.Count();
                double PACKNET, PACKGROSS,VVKG;
                int PACKBASE = 1, PACKNR;
                double tvalue = 0;
                string KRS, LFD, POS, LIEFSCH, VKPKDKOMM, GPASPPART, GPAFBPART, GPABEM, VKKBESTNR;
                string fkrs="", flfd="", fpos="";
                //VVVERSART,VVSPED
                bool IsVVSPED = false;
                DataRow dwVVD = null;
                foreach (var dwPOS in qVV)
                {
                    dwVVD = tbExport.Rows.Add();
                    //xStart=last row
                    KRS = dwPOS["VKPAUFKRS"].ToString();
                    LFD = dwPOS["VKPAUFLFD"].ToString();
                    POS = dwPOS["VKPAUFPOS"].ToString();
                    LIEFSCH = dwPOS["VVLIEFSCH"].ToString();
                    VKKBESTNR = dwPOS["VKKBESTNR"].ToString().Trim();
                    VKPKDKOMM = dwPOS["VKPKDKOMM"].ToString().Trim();
                    PACKNET = Convert.ToDouble(dwPOS["PACKNET"].ToString());
                    PACKGROSS = Convert.ToDouble(dwPOS["PACKGROSS"].ToString());
                    if (!IsVVSPED)
                    {
                        var qVVS = from DataRow row in db.Tables["VERSART"].AsEnumerable()
                                   where row["VVLIEFSCH"].ToString() == LIEFSCH
                                   select row;
                        foreach (DataRow row in qVVS)
                        {
                            tbMain.Rows[8]["F04"] = GetGB2312Value(row["VBEZ"].ToString().Trim());
                            tbMain.Rows[8]["F36"] = "电话:";
                            tbMain.Rows[8]["F40"] = GetGB2312Value(row["VTEL"].ToString().Trim());
                            //exAgent.SetCellValue(9, 4, GetGB2312Value(row["VBEZ"].ToString().Trim()));
                            //exAgent.SetCellValue(9, 40, row["VTEL"].ToString().Trim());
                            break;
                        }
                        IsVVSPED = true;
                    }
                    GPAFBPART = GetGB2312Value(dwPOS["GPAFBPART"].ToString());
                    GPASPPART = dwPOS["GPASPPART"].ToString();
                    GPABEM = GetGB2312Value(dwPOS["GPABEM"].ToString());
                    PACKNR = Convert.ToInt16(dwPOS["PACKNR"].ToString());
                    tbMain.Rows[xStart + rown * idx - 1]["F01"] = idx + 1;
                    tbMain.Rows[xStart + rown * idx - 1]["F60"] = Helper.GetSubRollNum(dwPOS["VVAVISNR"].ToString());
                    tbMain.Rows[xStart + rown * idx - 1]["F61"] = Helper.GetSubRollNum(LIEFSCH);
                    tbMain.Rows[xStart + rown * idx - 1]["F62"] = KRS + "-" + Helper.GetSubRollNum(LFD) + "-" + Helper.GetSubRollNum(POS);
                    /*
                     * exAgent.SetCellValue(xStart + rown * idx, 1, idx + 1);
                    exAgent.SetCellValue(xStart + rown * idx, 60, Helper.GetSubRollNum(dwPOS["VVAVISNR"].ToString()));
                    exAgent.SetCellValue(xStart + rown * idx, 61, Helper.GetSubRollNum(LIEFSCH));
                    exAgent.SetCellValue(xStart + rown * idx, 62, KRS + "-" + Helper.GetSubRollNum(LFD) + "-" + Helper.GetSubRollNum(POS));*/
                    dwVVD["F01"] = idx + 1;
                    dwVVD["F18"] = Helper.GetSubRollNum(dwPOS["VVAVISNR"].ToString());
                    dwVVD["F19"] = Helper.GetSubRollNum(LIEFSCH);
                    dwVVD["F20"] = KRS + "-" + Helper.GetSubRollNum(LFD) + "-" + Helper.GetSubRollNum(POS);
                    double gross = 0;
                    double net = 0;
                    net = System.Math.Round(Convert.ToDouble(dwPOS["VVKG"].ToString()), 2); tnet += net;
                    //gross = System.Math.Round(Convert.ToDouble(dwPOS["VVKG"].ToString()), 1);
                    gross = net + PACKNR * 0.4; tgross += gross;
                    tpackages += PACKNR;
                    //exAgent.SetCellValue(xStart + rown * idx, 11, gross);
                    //exAgent.SetCellValue(xStart + rown * idx, 15, net);
                    tbMain.Rows[xStart + rown * idx - 1]["F18"] = GPASPPART;
                    tbMain.Rows[xStart + rown * idx - 1]["F23"] = GPABEM.Replace(" ", "");
                    tbMain.Rows[xStart + rown * idx - 1]["F28"] = "件";
                    tbMain.Rows[xStart + rown * idx - 1]["F30"] = PACKNR;
                    tbMain.Rows[xStart + rown * idx - 1]["F32"] = PACKNET;
                    tbMain.Rows[xStart + rown * idx - 1]["F34"] = net;
                    tbMain.Rows[xStart + rown * idx - 1]["F41"] = PACKNET + 0.4;
                    tbMain.Rows[xStart + rown * idx - 1]["F43"] = gross;
                    tbMain.Rows[xStart + rown * idx - 1]["F47"] = GetGB2312Value(dwKD["KDKNAME"].ToString()).Trim();
                    tbMain.Rows[xStart + rown * idx - 1]["F51"] = VKKBESTNR;
                    if (VKKBESTNR.Trim() != VKPKDKOMM.Trim())
                    {
                        tbMain.Rows[xStart + rown * idx - 1]["F03"] = VKPKDKOMM;
                        dwVVD["F02"] = VKPKDKOMM;
                    }
                    else
                    {
                        //dwVVD["F02"] = VKKBESTNR;
                    }
                    dwVVD["F05"] = GPASPPART;
                    dwVVD["F06"] = GPABEM.Replace(" ", "");
                    dwVVD["F07"] = "件";
                    dwVVD["F08"] = PACKNR;
                    dwVVD["F09"] = PACKNET;
                    dwVVD["F10"] = net;

                    dwVVD["F12"] = PACKNET + 0.4;
                    dwVVD["F13"] = gross;
                    dwVVD["F14"] = GetGB2312Value(dwKD["KDKNAME"].ToString()).Trim();
                    dwVVD["F15"] = VKKBESTNR;
                    var qVKP = from DataRow row in dsSource.Tables["VKPOS"].AsEnumerable()
                               where row["VKPAUFKRS"].ToString() == KRS
                               && row["VKPAUFLFD"].ToString() == LFD
                               && row["VKPAUFPOS"].ToString() == POS
                               select row;
                    foreach (DataRow vkp in qVKP)
                    {
                        //    exAgent.SetCellValue(xStart + rown * idx, 1, idx + 1); 

                        string VKPPREH = vkp["VKPPREH"].ToString();
                        double VKPPREIS = Convert.ToDouble(vkp["VKPPREIS"].ToString()) * 0.001;
                        string VKPMEEH = vkp["VKPMEEH"].ToString();
                        if (idx == 0)
                        {
                            //tbMain.Rows[xStart + rown * idx-1]["F36"] =idx;
                            tbMain.Rows[15]["F35"] = "货物总值" + Environment.NewLine + "(" + VKPMEEH + ")";
                            //exAgent.SetCellValue(16, 37, "货物总值" + Environment.NewLine + "(" + VKPMEEH + ")");
                        }
                        tbMain.Rows[xStart + rown * idx - 1]["F08"] = GPAFBPART;
                        //exAgent.SetCellValue(xStart + rown * idx, 8, GPAFBPART);//vkp["VKPART"].ToString().Trim() + Environment.NewLine + GetGB2312Value(vkp["VKPKURZBEZ"].ToString().Trim()));
                        string color = GetGB2312Value(vkp["VKPFARBE"].ToString().Trim() + Environment.NewLine + "(" + vkp["VKPKDFARBNR"].ToString().Trim() + ")");

                        tbMain.Rows[xStart + rown * idx - 1]["F12"] = color;
                        //exAgent.SetCellValue(xStart + rown * idx, 12, color);
                        tbMain.Rows[xStart + rown * idx - 1]["F56"] = VKPPREIS;
                        //exAgent.SetCellValue(xStart + rown * idx, 56, VKPPREIS);
                        double linevalue = Math.Round(net * VKPPREIS, 8);
                        tvalue += linevalue;
                        tbMain.Rows[xStart + rown * idx - 1]["F37"] = linevalue;
                        //exAgent.SetCellValue(xStart + rown * idx, 37, linevalue);
                        dwVVD["F03"] = GPAFBPART;
                        dwVVD["F04"] = GetGB2312Value(vkp["VKPFARBE"].ToString().Trim() + "(" + vkp["VKPKDFARBNR"].ToString().Trim() + ")");
                        dwVVD["F16"] = VKPPREIS;
                        dwVVD["F11"] = linevalue;
                        break;
                    }
                    var qAB =
                             from DataRow row in dsSource.Tables["VKZUAB"].AsEnumerable()
                             where row["VKZAUFKRS"].ToString() == KRS
                             && row["VKZAUFLFD"].ToString() == LFD
                             && row["VKZAUFPOS"].ToString() == POS
                             && row["VVLIEFSCH"].ToString() == LIEFSCH
                             select row;
                    foreach (DataRow row in qAB)
                    {
                        double MADD = 0;
                        for (int d = 1; d < 6; d++)
                        {
                            MADD += Convert.ToDouble(row["VKZZUABWERT" + d].ToString()) * 0.01;
                        }
                        if (KRS == fkrs && LFD == flfd && POS == fpos)
                        {
                            break;
                        }
                        else
                        {
                            tbMain.Rows[xStart + rown * idx - 1]["F58"] = MADD;
                            //exAgent.SetCellValue(xStart + rown * idx, 58, MADD);
                            dwVVD["F17"] = MADD;
                            fkrs = KRS;
                            flfd = LFD;
                            fpos = POS;
                        }
                    }
                    idx++;
                    if (idx < count)
                    {
                        //exAgent.RangeCopy(18, 19, xStart + rown * idx, xStart + rown * idx + 1);
                    }
                }

                dwVVD = tbExport.Rows.Add();
                tbMain.Rows[1309]["F30"] = tpackages;
                tbMain.Rows[1309]["F34"] = Math.Round(tnet, 2);
                tbMain.Rows[1309]["F37"] = Math.Round(tvalue,2);
                tbMain.Rows[1309]["F43"] =Math.Round(tgross,2);
                /*
                exAgent.SetCellValue(1310, 30, tpackages, true);
                exAgent.SetCellValue(1310, 34, tnet, true);
                exAgent.SetCellValue(1310, 37, tvalue, true);
                exAgent.SetCellValue(1310, 43, tgross, true);*/
                dwVVD["F07"] = "总计:";
                dwVVD["F08"] = tpackages;
                dwVVD["F10"] = Math.Round(tnet, 2);
                dwVVD["F11"] = Math.Round(tvalue, 2);
                dwVVD["F13"] = Math.Round(tgross, 2);
                exAgent.SetCurrentSheet("YGRVV20LISTV");
                exAgent.RangeCopy(4, 4, 5, count + 4);
                exAgent.DataTableToExcel(tbExport, 4, 1);
                exAgent.SetCellValue(1, 1, 1);
                exAgent.BatchConvert2Number(4, 8, count + 3, 13);
                exAgent.BatchConvert2Number(4, 16, count + 3, 19);
                exAgent.SetCellValue(1, 1, date);
                exAgent.RenameSheet("明细");
                exAgent.SetCurrentSheet("YGRVV20LIST");
                exAgent.DataTableToExcel(tbMain, 1, 1);
                exAgent.ShowRows(xStart, 1, xStart + rown * count, 20);
                MergeSameX(18, count, rown);
                exAgent.RenameSheet(date);
            }
            catch (Exception ex)
            {
                msg = "有错误产生，请注意:";
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
            }
        }
        public void MergeSameX(int xStart, int rows, int nRow)
        {
            int x1, y1, x2, y2;
            string str = "";
            string str1;
            y1 = 5;
            y2 = 5;
            x1 = 4;
            x2 = 4;

            for (int i = 0; i < rows + 1; i++)
            {
                str1 = (string)exAgent.GetCellValue(xStart + i * nRow, 3) + (string)exAgent.GetCellValue(xStart + i * nRow, 8)
                    + (string)exAgent.GetCellValue(xStart + i * nRow, 12) + (string)exAgent.GetCellValue(xStart + i * nRow, 18);

                if (str == str1)
                {
                    x2 = xStart + i * nRow + nRow - 1;
                }
                else
                {
                    if (x2 > x1)
                    {
                        exAgent.Merge(x1, 3, x2, 3);
                        exAgent.Merge(x1, 8, x2, 8);
                        exAgent.Merge(x1, 12, x2, 12);
                        exAgent.Merge(x1, 18, x2, 18);
                        exAgent.Merge(x1, 23, x2, 23);
                        exAgent.Merge(x1, 28, x2, 28);
                    }
                    x1 = xStart + i * nRow;
                    str = str1;
                }
            }
        }
    }
}